#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    labNodeText = new QLabel("节点标题", this);
    labNodeText->setMinimumWidth(200);
    ui->statusbar->addWidget(labNodeText);

    spinRatio = new QSpinBox(this);
    spinRatio->setRange(0, 2000);
    spinRatio->setValue(100);
    spinRatio->setSuffix(" %");
    spinRatio->setReadOnly(true);
    spinRatio->setButtonSymbols(QAbstractSpinBox::NoButtons);
    ui->statusbar->addPermanentWidget(spinRatio);
    labFileName = new QLabel("文件名", this);
    ui->statusbar->addPermanentWidget(labFileName);

    BuildTreeHeader();
    InitTree();
    setCentralWidget(ui->scrollArea);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::BuildTreeHeader()
{
    ui->treeFiles_3->clear();
    QTreeWidgetItem* header = new QTreeWidgetItem();
    header->setText(MainWindow::colItem, "目录和文件");
    header->setText(MainWindow::colItemType, "节点类型");
    header->setText(MainWindow::colDate, "最后修改日期");

    header->setTextAlignment(MainWindow::colItem, Qt::AlignHCenter | Qt::AlignVCenter);
    header->setTextAlignment(MainWindow::colItemType, Qt::AlignHCenter | Qt::AlignVCenter);
    ui->treeFiles_3->setHeaderItem(header);
}

void MainWindow::InitTree()
{
    QIcon icon("F:\\Code\\Qt\\QTreeWidget4_12\\QTreeWidgetTest4_12\\icons\\15.ico");
    QTreeWidgetItem* item = new QTreeWidgetItem(MainWindow::itTopItem);
    item->setIcon(MainWindow::colItem, icon);

    item->setText(MainWindow::colItem, "图片");
    item->setText(MainWindow::colItemType, "Top Item");
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);

    item->setCheckState(MainWindow::colItem, Qt::Checked);
    ui->treeFiles_3->addTopLevelItem(item);
}

void MainWindow::AddFolderItem(QTreeWidgetItem *parItem, QString dir)
{
    QIcon icon("F:\\Code\\Qt\\QTreeWidget4_12\\QTreeWidgetTest4_12\\icons\\15.ico");
    QString NodeText = GetFinalFolderName(dir);
    QTreeWidgetItem* item = new QTreeWidgetItem(MainWindow::itGroupItem);
    item->setIcon(colItem, icon);
    item->setText(colItem, NodeText);
    item->setText(colItemType, "Group Item");
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
    item->setCheckState(colItem, Qt::Checked);
    item->setData(colDate, Qt::UserRole, QVariant(dir));
    parItem->addChild(item);
}

QString MainWindow::GetFinalFolderName(const QString &fullPathName)
{
    int cnt = fullPathName.length();
    int i = fullPathName.lastIndexOf("\\");
    QString str = fullPathName.right(cnt -i - 1);
    return str;
}

void MainWindow::AddImageItem(QTreeWidgetItem *parItem, QString fileName)
{
    QIcon icon("F:\\Code\\Qt\\QTreeWidget4_12\\QTreeWidgetTest4_12\\icons\\31.ico");
    QFileInfo fileInfo(fileName);
    QString nodeText = fileInfo.fileName();
    QDateTime birthDate = fileInfo.lastModified();

    QTreeWidgetItem *item;
    item = new QTreeWidgetItem(MainWindow::itImageItem);
    item->setIcon(colItem, icon);
    item->setText(colItem, nodeText);
    item->setText(colItemType, "Image Item");
    item->setText(colDate, birthDate.toString("yyyy-MM-dd"));
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
    item->setCheckState(colItem, Qt::Checked);
    item->setData(colDate, Qt::UserRole, QVariant(fileName));
    parItem->addChild(item);
}

void MainWindow::DeleteItem(QTreeWidgetItem *parItem, QTreeWidgetItem *item)
{
    if(item->childCount() > 0)
    {
        int count = item->childCount();
        QTreeWidgetItem *temParItem = item;
        for(int i = count - 1; i >= 0; i --)
            DeleteItem(temParItem, temParItem->child(i));
    }
    parItem->removeChild(item);
    delete item;
}

void MainWindow::ChangeItemCaption(QTreeWidgetItem *item)
{
    QString str = "*" + item->text(colItem);
    item->setText(colItem, str);
    if(item->childCount() > 0)
        for(int i = 0; i < item->childCount(); ++ i)
        {
            ChangeItemCaption(item->child(i));
        }
}

void MainWindow::displayImage(QTreeWidgetItem *item)
{
    QString fileName = item->data(colDate, Qt::UserRole).toString();
    labFileName->setText(fileName);
    labNodeText->setText(item->text(colItem));
    m_pixMap.load(fileName);
    ui->actZoomFitH->setEnabled(true);
    ui->actZoomFitW->setEnabled(true);
    ui->actZoomIn->setEnabled(true);
    ui->actZoomOut->setEnabled(true);
    ui->actZoomRealSize->setEnabled(true);
    ui->labPic_2->setPixmap(m_pixMap);
}

void MainWindow::on_actAddFolder_triggered()
{
    //QString curPath = QCoreApplication::applicationFilePath();
    QString dir = QFileDialog::getExistingDirectory();
    if(dir.isEmpty())
        return;
    QTreeWidgetItem *partItem = ui->treeFiles_3->currentItem();
    if(partItem == nullptr)
        return;
    if(partItem->type() != MainWindow::itImageItem)
        AddFolderItem(partItem, dir);
}

void MainWindow::on_actAddFiles_triggered()
{
    QStringList files =  QFileDialog::getOpenFileNames(this, "选择文件", "", "Imasge(*.jpg)");
    if(files.isEmpty())
        return;
    QTreeWidgetItem *parItem,  *item;
    item = ui->treeFiles_3->currentItem();
    if(item == nullptr)
        item = ui->treeFiles_3->topLevelItem(0);
    if(item->type() == itImageItem)
        parItem = item->parent();
    else
        parItem = item;
    for(int i = 0; i < files.size(); ++i)
    {
        QString aFileName = files.at(i);
        AddImageItem(parItem, aFileName);
    }
    parItem->setExpanded(true);
}

void MainWindow::on_treeFiles_3_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    //Q_UNUSED(previous);
    if(current == nullptr)
        return;
    if(current == previous)
        return;

    int var = current->type();
    switch(var)
    {
    case itTopItem:
        ui->actAddFolder->setEnabled(true);
        ui->actAddFiles->setEnabled(true);
        ui->actDeleteItem->setEnabled(false);
        break;
    case itGroupItem:
        ui->actAddFolder->setEnabled(true);
        ui->actAddFiles->setEnabled(true);
        ui->actDeleteItem->setEnabled(true);
        break;
    case itImageItem:
        ui->actAddFolder->setEnabled(true);
        ui->actAddFiles->setEnabled(false);
        ui->actDeleteItem->setEnabled(true);
        displayImage(current);
    }
}

void MainWindow::on_actDeleteItem_triggered()
{
    QTreeWidgetItem* item = ui->treeFiles_3->currentItem();
    if(item == nullptr)
        return;
    QTreeWidgetItem* paritem = item->parent();
    DeleteItem(paritem, item);
}

void MainWindow::on_actScanItems_triggered()
{
    for(int i = 0; i < ui->treeFiles_3->topLevelItemCount(); ++ i)
    {
        QTreeWidgetItem *item = ui->treeFiles_3->topLevelItem(i);
        ChangeItemCaption(item);
    }
}

void MainWindow::on_actZoomFitH_triggered()
{
    int H = ui->scrollArea->height();
    int real_H = m_pixMap.height();
    m_ratio = float(H) / real_H;
    spinRatio->setValue(m_ratio * 100);
    QPixmap pix = m_pixMap.scaledToHeight(H - 30);
    ui->labPic_2->setPixmap(pix);
}

void MainWindow::on_actZoomFitW_triggered()
{
    int W = ui->scrollArea->width();
    int real_W = m_pixMap.width();
    m_ratio = float(W) / real_W;
    spinRatio->setValue(m_ratio * 100);
    QPixmap pix = m_pixMap.scaledToWidth(W - 30);
    ui->labPic_2->setPixmap(pix);
}

void MainWindow::on_actZoomIn_triggered()
{
    m_ratio = m_ratio * 1.2;
    spinRatio->setValue(m_ratio);
    int w = m_ratio * m_pixMap.width();
    int h = m_ratio * m_pixMap.height();
    QPixmap pix = m_pixMap.scaled(w, h);
    ui->labPic_2->setPixmap(pix);
}

void MainWindow::on_actZoomOut_triggered()
{
    m_ratio = m_ratio * 0.8;
    spinRatio->setValue(m_ratio);
    int w = m_ratio * m_pixMap.width();
    int h = m_ratio * m_pixMap.height();
    QPixmap pix = m_pixMap.scaled(w, h);
    ui->labPic_2->setPixmap(pix);
}

void MainWindow::on_actZoomRealSize_triggered()
{
    m_ratio = 1;
    spinRatio->setValue(100);
    ui->labPic_2->setPixmap(m_pixMap);
}

void MainWindow::on_actDockFloat_triggered(bool checked)
{
    ui->dockWidget->setFloating(checked);
}

void MainWindow::on_actDockVisible_triggered(bool checked)
{
    ui->dockWidget->setVisible(checked);
}

